package from_0_to_50;

public class Q003_lengthOfLongestSubstring {
    public static void main(String[] args) {
        String str1 = "au";
        String str2 = "";
        String str3 = "abbcde";
        Integer maxLength1 = 0;
        Integer maxLength2 = 0;
        Integer maxLength3 = 0;
        Q003_lengthOfLongestSubstring lengthOfLongestSubstring = new Q003_lengthOfLongestSubstring();
        maxLength1 = lengthOfLongestSubstring.lengthOfLongestSubstring(str1);
        maxLength2 = lengthOfLongestSubstring.lengthOfLongestSubstring(str2);
        maxLength3 = lengthOfLongestSubstring.lengthOfLongestSubstring(str3);
        System.out.println("maxLength1::"+maxLength1);
        System.out.println("maxLength2::"+maxLength2);
        System.out.println("maxLength3::"+maxLength3);
    }

    public int lengthOfLongestSubstring(String s) {
        int max = 1;
        int length = s.length();
        System.out.println(length);
        int dp[] = new int[length];
        for (int l = 0; l <length; l++) {
            dp[l] = 1;
        }
        // 窗口+dp
        for (int i = 1; i < length; i++) {
            for (int j = 1; j <= dp[i-1]; j++) {
                if(s.charAt(i) == s.charAt(i-j)){
                    dp[i] = j;
                    break;
                }else
                    dp[i] = dp[i-1] + 1;

            }
            max = Math.max(max,dp[i]);
        }
        return max = length == 0 ? 0 : max ;
    }
}
